{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "individual-voltage",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://quotes.money.163.com/service/chddata.html?code=0600895&start=20190101&end=20190110&fields=TCLOSE;HIGH;LOW;TOPEN;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER\n"
     ]
    }
   ],
   "source": [
    "import urllib.request\n",
    "stockCode = '600895'\n",
    "url = 'http://quotes.money.163.com/service/chddata.html?code=0600895&start=20190101&end=20190110&fields=TCLOSE;HIGH;LOW;TOPEN;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER'\n",
    "print(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "nominated-click",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "日期,股票代码,名称,收盘价,最高价,最低价,开盘价,涨跌额,涨跌幅,换手率,成交量,成交金额\r\n",
      "2019-01-10,'600895,张江高科,15.2,16.11,15.12,15.88,-0.82,-5.1186,3.4353,53202090,829197046.0\r\n",
      "2019-01-09,'600895,张江高科,16.02,16.33,15.75,16.02,-0.02,-0.1247,3.3991,52641127,844641306.0\r\n",
      "2019-01-08,'600895,张江高科,16.04,16.56,15.81,16.28,-0.25,-1.5347,3.5851,55522302,895511540.0\r\n",
      "2019-01-07,'600895,张江高科,16.29,16.65,16.03,16.2,-0.01,-0.0613,3.8241,59222671,969185655.0\r\n",
      "2019-01-04,'600895,张江高科,16.3,16.58,15.6,15.7,0.06,0.3695,4.4545,68985635,1109319288.0\r\n",
      "2019-01-03,'600895,张江高科,16.24,16.65,15.31,15.78,0.31,1.946,6.117,94733382,1522054615.0\r\n",
      "2019-01-02,'600895,张江高科,15.93,16.33,14.71,15.06,0.98,6.5552,4.9061,75979904,1188520419.0\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "response = urllib.request.urlopen(url)\n",
    "print(response.read().decode(\"gbk\"))\n",
    "response.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "proper-julian",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "日期,股票代码,名称,收盘价,最高价,最低价,开盘价,涨跌额,涨跌幅,换手率,成交量,成交金额\r\n",
      "2019-02-01,'600895,张江高科,14.2,14.24,13.87,13.91,0.4,2.8986,1.2621,19546288,274647248.0\r\n",
      "2019-01-31,'600895,张江高科,13.8,14.75,13.58,14.69,-0.57,-3.9666,2.1112,32695437,462076814.0\r\n",
      "2019-01-30,'600895,张江高科,14.37,14.77,14.33,14.49,-0.17,-1.1692,1.0508,16274136,236429070.0\r\n",
      "2019-01-29,'600895,张江高科,14.54,15.5,14.18,15.27,-0.81,-5.2769,2.0276,31401261,464392301.0\r\n",
      "2019-01-28,'600895,张江高科,15.35,15.57,15.21,15.5,0.02,0.1305,1.4057,21769886,336197080.0\r\n",
      "2019-01-25,'600895,张江高科,15.33,16.04,15.27,15.92,-0.74,-4.6049,2.7233,42175769,658004972.0\r\n",
      "2019-01-24,'600895,张江高科,16.07,16.65,15.93,16.65,-0.33,-2.0122,2.5478,39457212,639328054.0\r\n",
      "2019-01-23,'600895,张江高科,16.4,16.68,15.91,16.36,0.04,0.2445,2.5951,40190374,654543971.0\r\n",
      "2019-01-22,'600895,张江高科,16.36,16.91,16.22,16.3,-0.04,-0.2439,3.0405,47087722,780105902.0\r\n",
      "2019-01-21,'600895,张江高科,16.4,16.53,15.92,16.22,0.11,0.6753,2.4973,38675827,628198092.0\r\n",
      "2019-01-18,'600895,张江高科,16.29,16.8,16.05,16.72,-0.69,-4.0636,4.0162,62198832,1015749203.0\r\n",
      "2019-01-17,'600895,张江高科,16.98,17.05,15.6,15.6,1.27,8.084,5.5731,86309543,1421888187.0\r\n",
      "2019-01-16,'600895,张江高科,15.71,16.17,15.46,15.75,0.17,1.094,2.8871,44711686,707251057.0\r\n",
      "2019-01-15,'600895,张江高科,15.54,15.64,15.09,15.4,0.08,0.5175,2.0461,31687291,486873365.0\r\n",
      "2019-01-14,'600895,张江高科,15.46,16.08,15.35,15.67,-0.1,-0.6427,2.793,43255147,681398110.0\r\n",
      "2019-01-11,'600895,张江高科,15.56,15.8,15.06,15.2,0.36,2.3684,2.7157,42057493,651939200.0\r\n",
      "2019-01-10,'600895,张江高科,15.2,16.11,15.12,15.88,-0.82,-5.1186,3.4353,53202090,829197046.0\r\n",
      "2019-01-09,'600895,张江高科,16.02,16.33,15.75,16.02,-0.02,-0.1247,3.3991,52641127,844641306.0\r\n",
      "2019-01-08,'600895,张江高科,16.04,16.56,15.81,16.28,-0.25,-1.5347,3.5851,55522302,895511540.0\r\n",
      "2019-01-07,'600895,张江高科,16.29,16.65,16.03,16.2,-0.01,-0.0613,3.8241,59222671,969185655.0\r\n",
      "2019-01-04,'600895,张江高科,16.3,16.58,15.6,15.7,0.06,0.3695,4.4545,68985635,1109319288.0\r\n",
      "2019-01-03,'600895,张江高科,16.24,16.65,15.31,15.78,0.31,1.946,6.117,94733382,1522054615.0\r\n",
      "2019-01-02,'600895,张江高科,15.93,16.33,14.71,15.06,0.98,6.5552,4.9061,75979904,1188520419.0\r\n",
      "\n"
     ]
    }
   ],
   "source": [
    "baseurl='http://quotes.money.163.com/service/chddata.html'\n",
    "\n",
    "param = bytes(urllib.parse.urlencode({'code:':'0'+stockCode, 'start':'20190102', 'end':'20190210','fields':'TCLOSE;HIGH;LOW;TOPEN;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER'}), encoding='utf8')\n",
    "response = urllib.request.urlopen(url, data=param, timeout=1)\n",
    "print(response.read().decode(\"gbk\"))\n",
    "response.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "activated-inspiration",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getAndSaveStock(stockCodeList, path):\n",
    "    for stockCode in stockCodeList:\n",
    "        baseurl='http://quotes.money.163.com/service/chddata.html'\n",
    "        param = bytes(urllib.parse.urlencode({'code:':'0'+stockCode, 'start':'20190102', 'end':'20190210','fields':'TCLOSE;HIGH;LOW;TOPEN;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER'})\n",
    "                      , encoding='utf8')\n",
    "        urllib.request.urlretrieve(url, path+stockCode+'.csv', data=param)\n",
    "stockCodeList=[]\n",
    "stockCodeList.append('600895')\n",
    "stockCodeList.append('600007')\n",
    "getAndSaveStock(stockCodeList,'../datasets/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dependent-thunder",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas_datareader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "published-cleveland",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 High        Low       Open      Close    Volume  Adj Close\n",
      "Date                                                                       \n",
      "2021-01-04  17.150000  16.830000  17.020000  17.040001  13930091  17.040001\n",
      "2021-01-05  17.080000  16.790001  17.040001  17.080000  12051733  17.080000\n",
      "2021-01-06  17.670000  17.040001  17.080000  17.320000  15817079  17.320000\n",
      "2021-01-07  17.350000  16.709999  17.320000  16.820000  13134282  16.820000\n",
      "2021-01-08  17.950001  16.840000  16.879999  17.660000  22842631  17.660000\n",
      "2021-01-11  17.610001  17.170000  17.510000  17.180000  13392448  17.180000\n",
      "2021-01-12  17.469999  17.150000  17.230000  17.180000  11226253  17.180000\n",
      "2021-01-13  18.000000  17.299999  17.400000  17.700001  25442563  17.700001\n",
      "2021-01-14  18.430000  17.469999  17.620001  18.200001  30855396  18.200001\n",
      "2021-01-15  18.379999  17.709999  18.379999  17.900000  16900652  17.900000\n",
      "2021-01-18  18.129999  17.780001  17.850000  17.900000  14721229  17.900000\n",
      "2021-01-19  18.010000  17.549999  17.850000  17.790001  15058286  17.790001\n",
      "2021-01-20  17.750000  17.500000  17.750000  17.570000   9471644  17.570000\n",
      "2021-01-21  17.690001  17.450001  17.590000  17.520000  11229894  17.520000\n",
      "2021-01-22  17.459999  17.010000  17.459999  17.080000  12900002  17.080000\n",
      "2021-01-25  17.350000  16.799999  17.059999  17.250000  13843144  17.250000\n",
      "2021-01-26  17.230000  16.809999  17.160000  16.820000   8703741  16.820000\n",
      "2021-01-27  16.969999  16.809999  16.809999  16.830000   5364031  16.830000\n",
      "2021-01-28  17.969999  17.010000  17.010000  17.420000  25089717  17.420000\n",
      "2021-01-29  17.850000  17.160000  17.639999  17.260000  16388574  17.260000\n"
     ]
    }
   ],
   "source": [
    "#!pip install pandas_datareader\n",
    "code='600895.ss'\n",
    "stock = pandas_datareader.get_data_yahoo(code, '2021-01-01', '2021-01-30')\n",
    "print(stock)\n",
    "\n",
    "# 绘制K线图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "anticipated-factor",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock.to_csv('../datasets/600895.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "agreed-picking",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "qualified-depression",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
